package com.example.xgakj.newterminal.ui.activities;

import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;

import com.example.xgakj.mylibrary.mvp.utils.ToastUtil;
import com.example.xgakj.newterminal.R;
import com.socks.library.KLog;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Playmp4Activity extends AppCompatActivity implements MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {

    @BindView(R.id.videoView)
    VideoView mVideoView;
    private Uri mUri;
    private MediaController mMediaController;
    private int mPositionWhenPaused = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_playmp4);
        //Set the screen to landscape.
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        ButterKnife.bind(this);
        String path = getIntent().getStringExtra("path");
        KLog.e(path);
        //Video file
        mUri = Uri.parse(path);
        //Create media controller，组件可以控制视频的播放，暂停，回复，seek等操作，不需要你实现
        mMediaController = new MediaController(this);
        mVideoView.setMediaController(mMediaController);
        mMediaController.setKeepScreenOn(true);

    }
    @Override
    public void onStart() {
        // Play Video
        mVideoView.setVideoURI(mUri);
        mVideoView.start();

        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mVideoView.stopPlayback();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMediaController=null;
        mVideoView=null;
    }

    @Override
    public void onPause() {
        // Stop video when the activity is pause.
        mPositionWhenPaused = mVideoView.getCurrentPosition();
        mVideoView.stopPlayback();

        super.onPause();
    }
    @Override
    public void onResume() {
        // Resume video player
        if(mPositionWhenPaused >= 0) {
            mVideoView.seekTo(mPositionWhenPaused);
            mPositionWhenPaused = -1;
        }

        super.onResume();
    }
    @Override
    public boolean onError(MediaPlayer player, int arg1, int arg2) {
        ToastUtil.showShort(getApplicationContext(),"视频出错，无法播放");
        return false;
    }
    @Override
    public void onCompletion(MediaPlayer mp) {

    }
}
